package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestSuccessInvokeRamdomRTController {
  @Autowired
  private UserService userService;
  
  @RequestMapping({"/randomRT/{id}"})
  @ResponseBody
  public User selectUser(@PathVariable String id) throws Exception {
    User user = this.userService.queryAndUpdateUser(id);
    long ramdomSleepTimeMS = (long)(Math.random() * 10.0D);
    Thread.sleep(ramdomSleepTimeMS * 1000L);
    send("https://www.aliyun.com/");
    return user;
  }
  
  private String send(String url) throws Exception {
    GetMethod getMethod = new GetMethod(url);
    HttpClient client = new HttpClient();
    client.executeMethod((HttpMethod)getMethod);
    System.out.println(getMethod.getStatusLine());
    getMethod.releaseConnection();
    return getMethod.getResponseBodyAsString();
  }
}
